import 'package:fish_redux/fish_redux.dart';
import '../../../app_files.dart';
import 'action.dart';
import 'state.dart';
import '../BindAccountView/AccountInfoView/state.dart';
import '../model/account_model.dart';

Effect<SearchAccountViewState> buildEffect() {
  return combineEffects(<Object, Effect<SearchAccountViewState>>{
    SearchAccountViewAction.action: _onAction,
    SearchAccountViewAction.onSubmit: _onSubmit,
  });
}

void _onAction(Action action, Context<SearchAccountViewState> ctx) {}

void _onSubmit(Action action, Context<SearchAccountViewState> ctx) async {
  String errText = '';
//  if (ctx.state.accountController.text.length == 0) {
//    errText = '请输入燃气表号';
//  }
  if (ctx.state.phoneController.text.length == 0) {
    errText = '请输入手机号码';
  }
  if (ctx.state.nameController.text.length == 0) {
    errText = '请输入户主姓名';
  }
  if (errText.length != 0) {
    CommonUtils.showErrorInfo(ctx.context, errText);
    return;
  }
  CommonUtils.closeKey(ctx.context);
  CommonUtils.showLoadingDialog(ctx.context);
  ResultData res = await CommonNetWork.gasAccountSearch(
      userName: ctx.state.nameController.text,
      userMobile: ctx.state.phoneController.text);
  Navigator.pop(ctx.context);
  if (res.success) {
    List list = res.data;
    List<AccountItemModel> items = list
        .map<AccountItemModel>((e) => AccountItemModel.fromJson(e))
        .toList();
    var popRes = await CommonUtils.pushRoute(ctx.context, RoutePath.gasNoSearchResultView,
        arguments: {
          'items': items,
          'isEmpty': false,
        });
    if (popRes == '成功') {
      Navigator.pop(ctx.context,'成功');
    }
  } else {
    if (res.desc == '未查询到客户信息') {
      CommonUtils.pushRoute(ctx.context, RoutePath.gasNoSearchResultView,
          arguments: {'isEmpty': true});
    } else
      CommonUtils.showErrorInfo(ctx.context, res.desc);
  }
}
